class TrainTask < ActiveRecord::Base
  has_many :maintenances , :dependent => :destroy
  belongs_to :train
  has_many :checkrecords, :dependent => :destroy
  
  def self.findBySn sn
    find(:first,:conditions => ['sn = ?',sn])
  end

  def self.findPageByTrainId(offset, limit ,train_id)
    find(:all,
      :offset     => offset,
      :limit      => limit,
      :conditions => ['train_id = :train_id', {:train_id => train_id}],
      :order      => 'begin_time DESC')
  end

  def self.findPage(offset, limit)
    find(:all,
      :offset     => offset,
      :limit      => limit,
      :order      => 'begin_time DESC')
  end

  def self.findAllByIsEnd is_end
    find(:all,
      :conditions => ['is_end = ?',is_end],
      :order      => 'begin_time DESC')
  end
  
  #根据train_task_id来数记录
  def self.countByTrainId(train_id)
    count(:conditions => ['train_id = :train_id', {:train_id => train_id}])
  end

  #根据is_end得到记录数
  def self.countByIsEnd is_end
    count(:conditions => ['is_end = ?',is_end])
  end

  def self.getTrainByCheckTime check_time
    find(
      :first,
      :conditions => ['begin_time < ?',check_time],
      :order => 'begin_time DESC'
    )
  end
  
end
